import sys
from PySide6.QtWidgets import (QApplication, QWidget, QMainWindow, QLabel,
                               QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout,
                               QMessageBox)
from PySide6.QtCore import Qt

# 模拟数据库存储用户信息
users = {
    "admin": "admin123"
}


class LoginWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("登录")
        self.setFixedSize(300, 200)

        # 创建主部件和布局
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        layout = QVBoxLayout(main_widget)

        # 用户名输入
        self.username_input = QLineEdit()
        self.username_input.setPlaceholderText("用户名")
        layout.addWidget(self.username_input)

        # 密码输入
        self.password_input = QLineEdit()
        self.password_input.setPlaceholderText("密码")
        self.password_input.setEchoMode(QLineEdit.Password)
        layout.addWidget(self.password_input)

        # 按钮布局
        btn_layout = QHBoxLayout()

        # 登录按钮
        login_btn = QPushButton("登录")
        login_btn.clicked.connect(self.attempt_login)
        btn_layout.addWidget(login_btn)

        # 注册按钮
        register_btn = QPushButton("注册")
        register_btn.clicked.connect(self.show_register)
        btn_layout.addWidget(register_btn)

        layout.addLayout(btn_layout)

    def attempt_login(self):
        username = self.username_input.text()
        password = self.password_input.text()

        if not username or not password:
            QMessageBox.warning(self, "错误", "用户名和密码不能为空！")
            return

        if username in users and users[username] == password:
            QMessageBox.information(self, "成功", f"欢迎回来，{username}！")
            # 这里可以跳转到主界面
        else:
            QMessageBox.critical(self, "错误", "用户名或密码错误！")

    def show_register(self):
        self.register_window = RegisterWindow(self)  # 传递父窗口引用
        self.register_window.show()
        self.hide()


class RegisterWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent_window = parent
        self.setWindowTitle("注册")
        self.setFixedSize(300, 200)

        layout = QVBoxLayout(self)

        # 用户名输入
        self.username_input = QLineEdit()
        self.username_input.setPlaceholderText("用户名")
        layout.addWidget(self.username_input)

        # 密码输入
        self.password_input = QLineEdit()
        self.password_input.setPlaceholderText("密码")
        self.password_input.setEchoMode(QLineEdit.Password)
        layout.addWidget(self.password_input)

        # 确认密码
        self.confirm_input = QLineEdit()
        self.confirm_input.setPlaceholderText("确认密码")
        self.confirm_input.setEchoMode(QLineEdit.Password)
        layout.addWidget(self.confirm_input)

        # 按钮布局
        btn_layout = QHBoxLayout()

        # 提交按钮
        submit_btn = QPushButton("提交")
        submit_btn.clicked.connect(self.attempt_register)
        btn_layout.addWidget(submit_btn)

        # 返回按钮
        back_btn = QPushButton("返回")
        back_btn.clicked.connect(self.back_to_login)
        btn_layout.addWidget(back_btn)

        layout.addLayout(btn_layout)

    def attempt_register(self):
        username = self.username_input.text()
        password = self.password_input.text()
        confirm = self.confirm_input.text()

        if not username or not password:
            QMessageBox.warning(self, "错误", "用户名和密码不能为空！")
            return

        if password != confirm:
            QMessageBox.critical(self, "错误", "两次输入的密码不一致！")
            return

        if username in users:
            QMessageBox.critical(self, "错误", "用户名已存在！")
            return

        # 注册成功
        users[username] = password
        QMessageBox.information(self, "成功", "注册成功！")
        self.back_to_login()

        # 自动填充登录界面
        self.parent_window.username_input.setText(username)
        self.parent_window.password_input.clear()

    def back_to_login(self):
        self.parent_window.show()
        self.close()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = LoginWindow()
    window.show()
    sys.exit(app.exec())